package com.cjst.service.impl.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cjst.dto.SysDictDto;
import com.cjst.pojo.sys.SysDict;
import com.cjst.service.impl.base.BaseServiceImpl;
import com.cjst.service.sys.SysDictService;
import com.cjst.sql.C;
import com.cjst.sql.Method;
import com.cjst.sql.WherePrams;
import com.cjst.util.common.GenerativeToken;

/**
 * 字典服务层
 * @author szbj001
 *
 */
@Service
public class SysDictServiceImpl extends BaseServiceImpl<SysDict, String> implements SysDictService{
	

	/**
	 * 新增字典，并增加字典相关的值
	 */
	@Transactional
	public boolean addDict(SysDict dict,String dictValues){
		String dictId = GenerativeToken.getToken().toUpperCase();
		dict.setNum("0");
		dict.setPid("0");
		dict.setId(dictId);
		this.addLocal(dict);
		if(StringUtils.isNotEmpty(dictValues)){
			String[] dicts = dictValues.split(";");
			for (String str : dicts) {
				String[] ss = str.split(":");
				SysDict dd = new SysDict();
				dd.setPid(dictId);
				dd.setNum(ss[0]);
				dd.setName(ss[1]);
				dd.setType(dict.getType());
				this.addLocalByid(dd);
			}
		}
		return true;		
	}
	
	/**
	 * 修改字典属性和值
	 */
	@Transactional
	public boolean updateDict(SysDict dict,String dictValues){
		
		//修改字典属性
		this.update(dict);
		//删除值
		WherePrams wherePrams = Method.where("pid", C.EQ, dict.getId());
		this.del(wherePrams);
		//重新加值
		if(StringUtils.isNotEmpty(dictValues)){
			
			String[] dicts = dictValues.split(";");
			for (String str : dicts) {
				String[] ss = str.split(":");
				SysDict dd = new SysDict();
				dd.setPid(dict.getId());
				dd.setNum(ss[0]);
				dd.setName(ss[1]);
				dd.setType(dict.getType());
				this.addLocalByid(dd);
			}
		}
		return true;
	}
	
	/**
	 * 根据字典类型和字典的key值，返回value
	 */
	public String getDictVal(String dictType,String dictKey) {
		if(StringUtils.isNotEmpty(dictType)&&StringUtils.isNotEmpty(dictKey)) {
			WherePrams prams = Method.where("delFlag", C.EQ, "0");
			prams.and("type", C.EQ, dictType);
			prams.and("num", C.EQ, dictKey);
			prams.and("pid", C.NE, "0");
			SysDict dict = this.get(prams);
			if(dict!=null) {
				return dict.getName();
			}
			
		}
		return null;
	}
	
	/**
	 * 根据字典类型查询所有的键值
	 */
	public List<Map<String, String>> getDicts(String dictType){
		List<Map<String, String>> maps = new ArrayList<>();
		if(StringUtils.isNotEmpty(dictType)) {
			WherePrams prams = Method.where("delFlag", C.EQ, "0");
			prams.and("type", C.EQ, dictType);
			prams.and("pid", C.NE, "0");
			prams.orderBy(" num asc ");
			List<SysDict> dicts = this.list(prams);
			
			if(dicts!=null&&dicts.size()>0) {
				for (SysDict sysDict : dicts) {
					Map<String, String> map = new HashMap<>();
					map.put("key", sysDict.getNum());
					map.put("value", sysDict.getName());
					
					maps.add(map);
				}
			}
			
		}
		return maps;
	}
	/**
	 * 根据字典类型查询所有的键值
	 */
	public List<SysDict> getDicts(String dictType,int flag){
		List<SysDict> list = new ArrayList<SysDict>();
		if(StringUtils.isNotEmpty(dictType)) {
			if(flag==0){
				WherePrams prams = Method.where("delFlag", C.EQ, "0");
				prams.and("type", C.EQ, dictType);
				prams.and("pid", C.NE, "0");
				prams.orderBy(" num asc ");
				list = this.list(prams);
				
			
			}else if(flag==1){
				WherePrams prams = Method.where("delFlag", C.EQ, "0");
				prams.and("type", C.LIKE, dictType,1);
				prams.and("pid", C.EQ, "0");
				prams.orderBy(" num asc ");
				list = this.list(prams);
				
			}else if(flag==2){
				WherePrams prams = Method.where("delFlag", C.EQ, "0");
				prams.and("pid", C.EQ, dictType);
				prams.orderBy(" num asc ");
				list = this.list(prams);
			}else if(flag==3){
				WherePrams prams = Method.where("delFlag", C.EQ, "0");
				prams.and("id", C.EQ, dictType);
				prams.orderBy(" num asc ");
				list = this.list(prams);
			}
		}
		return list;
	}
	public List<SysDictDto> getDictsDto(String dictType,int flag){
		List<SysDictDto> list = new ArrayList<SysDictDto>();
		if(StringUtils.isNotEmpty(dictType)) {
			if(flag==0){
				WherePrams prams = Method.where("delFlag", C.EQ, "0");
				prams.and("type", C.EQ, dictType);
				prams.and("pid", C.NE, "0");
				prams.orderBy(" num asc ");
				List<SysDict> listDto = this.list(prams);
				if(listDto!=null && listDto.size()>0){
					for (SysDict sysDict : listDto) {
						SysDictDto sDto=new SysDictDto();
						sDto.setId(sysDict.getId());
						sDto.setName(sysDict.getName());
						sDto.setNum(sysDict.getNum());
						sDto.setPid(sysDict.getPid());
						sDto.setTips(sysDict.getTips());
						sDto.setType(sysDict.getType());
						sDto.setDelFlag(sysDict.getDelFlag());
						list.add(sDto);
					}
				}
			
			}else if(flag==1){
				WherePrams prams = Method.where("delFlag", C.EQ, "0");
				prams.and("type", C.LIKE, dictType,1);
				prams.and("pid", C.EQ, "0");
				prams.orderBy(" num asc ");
				List<SysDict> listDto = this.list(prams);
				if(listDto!=null && listDto.size()>0){
					for (SysDict sysDict : listDto) {
						SysDictDto sDto=new SysDictDto();
						sDto.setId(sysDict.getId());
						sDto.setName(sysDict.getName());
						sDto.setNum(sysDict.getNum());
						sDto.setPid(sysDict.getPid());
						sDto.setTips(sysDict.getTips());
						sDto.setType(sysDict.getType());
						sDto.setDelFlag(sysDict.getDelFlag());
						list.add(sDto);
					}
				}
				
			}else if(flag==2){
				WherePrams prams = Method.where("delFlag", C.EQ, "0");
				prams.and("pid", C.EQ, dictType);
				prams.orderBy(" num asc ");
				List<SysDict> listDto = this.list(prams);
				if(listDto!=null && listDto.size()>0){
					for (SysDict sysDict : listDto) {
						SysDictDto sDto=new SysDictDto();
						sDto.setId(sysDict.getId());
						sDto.setName(sysDict.getName());
						sDto.setNum(sysDict.getNum());
						sDto.setPid(sysDict.getPid());
						sDto.setTips(sysDict.getTips());
						sDto.setType(sysDict.getType());
						sDto.setDelFlag(sysDict.getDelFlag());
						list.add(sDto);
					}
				}
			}
		}
		return list;
	}
	
}
